package com.server.dao;

import com.server.model.Login;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


public class LoginDAO extends GenericDAO{
    
   public void create(Login login){
      String sql = "INSERT INTO Login (Id, senha, perfil) values (" ;
               sql += login.getUsuario() + ",'" + login.getSenha() + "'" +
              ", '" +  login.getPerfil()+ ")";
               
               try{
                   Connection con = super.getConnection();
                   Statement stmt = super.createStatement();
                   int res = stmt.executeUpdate(sql);
                           
               }catch(SQLException sqle){
                 sqle.printStackTrace();
               }
        
    }
    
     public void delete(Login login){
        
      String sql = "DELETE FROM Login WHERE  Id = " +  login.getUsuario() ;
               
               try{
                   Connection con = super.getConnection();
                   Statement stmt = super.createStatement();
                   int res = stmt.executeUpdate(sql);
                           
               }catch(SQLException sqle){
                 sqle.printStackTrace();
               }
        
    }
    
    public void update(Login login,int novo_id, 
                       String senha, String perfil){
        
        String sql = "UPDATE Login SET Id =" + novo_id + ","
                + "Senha = '" + senha 
                + "Perfil = '" + perfil
                + "' WHERE  Id = " +  login.getUsuario();
               
               try{
                   Connection con = super.getConnection();
                   Statement stmt = super.createStatement();
                   int res = stmt.executeUpdate(sql);
                           
               }catch(SQLException sqle){
                 sqle.printStackTrace();
               }
        
    }
    
    public boolean selectUsingId (int id, String senha){
        
         String sql = "SELECT * FROM Login WHERE Id = " + id + " and senha = " + senha;
               
               try{
                   Connection con = super.getConnection();
                   Statement stmt = super.createStatement();
                   ResultSet res = stmt.executeQuery(sql);
                   
                   if (res.next()){
                       return true;
                     }
                   else {
                       return false;
                   }
                           
               }catch(SQLException sqle){
                 sqle.printStackTrace();
               }
        return false;
    }

     public Login ValidaUsuarioSenha (Login login){
        
         String sql = "SELECT * FROM Login WHERE Usuario = '" + login.getUsuario() + "' and senha = '" + login.getSenha() + "';";
               
               try{
                   Connection con = super.getConnection();
                   Statement stmt = super.createStatement();
                   ResultSet res = stmt.executeQuery(sql);
                   
                   if (res.next()){
                       
                  //   while (res.next()){
                       login.setPerfil(res.getInt("Perfil"));
                    //    }  
                       
                       return login;
                     }
                   else {
                       return null;
                   }
                           
               }catch(SQLException sqle){
                 sqle.printStackTrace();
               }
        return null;
    }
     
    
    
}